<?php
/**
 * @title 前序遍历
 * @author start2004
 */

// ini_set("memory_limit", "128M");

class Solution {

    /**
     * @title 序列化
     * @author start2004
     * @since 2020-09-09 Created
     *
     * @param $root TreeNode
     * @return array
     */
    function serialize($root) {
        if($root === null){
            return [];
        } else {}

        /**
         * @since 2020-09-09 root-left-right
         */
        $result = [$root->val];

        /**
         * @since 2020-09-09 左右子树
         */
        if($root->left === null){
            $result[] = null;
        } else {
            $result = array_merge($result, self::serialize($root->left));
        }
        if($root->right === null){
            $result[] = null;
        } else {
            $result = array_merge($result, self::serialize($root->right));
        }

        /**
         * @return
         */
        return $result;
    }

    public

    /**
     * @title 反序列化
     * @author start2004
     * @since 2020-09-09 Created
     *
     * @param array $list
     * @return TreeNode
     */
    function deserialize($list = []){
        /**
         * @since 2020-09-09 长度
         */
        $len = count($list);
        if($len == 0){
            return null;
        } else {}

        /**
         * @since 2020-09-09 根节点
         */
        $root = new TreeNode($list[0]);
        $nodes = [];
        $node = $root;

        /**
         * @since 2020-09-09 左右节点标志
         */
        $left = true;

        /**
         * @since 2020-09-09 迭代
         */
        for ($i=1; $i<$len; $i++){
            $val = $list[$i];

            /**
             * @since 2020-09-09 null
             */
            if($val === null){
                if($left){
                    /**
                     * @since 2020-09-09 转移到右节点
                     */
                    $left = false;
                } else {
                    /**
                     * @since 2020-09-09 已经是右节点, 抛出上一级父节点
                     */
                    $node = array_pop($nodes);
                }
            } else {
                /**
                 * @since 2020-09-09 新节点
                 */
                $leaf = new TreeNode($val);

                /**
                 * @since 2020-09-09 左右节点
                 */
                if($left){
                    $node->left = $leaf;
                    $nodes[] = $node;
                } else {
                    $node->right = $leaf;
                    /**
                     * @since 2020-09-09 右节点出现后, node不可能再作为父节点, 不能入队列
                     */
                }

                /**
                 * @since 2020-09-09 子节点成为新的父节点
                 */
                $node = $leaf;

                /**
                 * @since 2020-09-09 重置左节点方向
                 */
                $left = true;
            }
        }

        /**
         * @return
         */
        return $root;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202009/2020.09.09_2.php
 */
$datas = [
    // [1,2,3,null,null,4,5],
    [0,1,2,3,4,null,6,null,null,null,null,13],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->serialize($xhprof->tree($data));
    foreach ($result as $r){
        if($r === null){
            echo "null, ";
        } else {
            echo $r, ", ";
        }
    }
    echo str_repeat(PHP_EOL, 2);
    $result = $obj->deserialize($result);
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". $data[1][$i][0] ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
